<?php

class Admin_CharacterSetupController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	$this->view->headScript()->appendFile($this->view->baseUrl().'/js/jquery-1.8.1.min.js');
    }

    public function indexAction()
    {
        $config=new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini",'production');
		$db = Zend_Db::factory($config->resources->db);
		$select=$db->select()->from(array('a'=>'txy_taobao_items'))->joinLeft(array('b'=>'txy_item_characters_list'),'a.num_iid=b.numiid')->where('b.numiid is null and a.is_onsale<100');
		$adapter=new Zend_Paginator_Adapter_DbSelect($select);
		$paginator = new Zend_Paginator($adapter);
		$paginator->setCurrentPageNumber($this->_getParam('page'));
		$this->view->paginator = $paginator;
		$characterSelect=$db->select()->from('txy_item_characters');
		$characterRes=$db->fetchAll($characterSelect);
		$level0=array();
		$level1=array();
		$level2=array();
		$level3=array();
		$level4=array();
		foreach($characterRes as $res){
			if(0==$res['level']){
				$level0[]=$res;
			}
			if(1==$res['level']){
				$level1[]=$res;
			}
			if(2==$res['level']){
				$level2[]=$res;
			}
			if(3==$res['level']){
				$level3[]=$res;
			}
			if(4==$res['level']){
				$level4[]=$res;
			}
		}
		$this->view->level0=$level0;
		$this->view->level1=$level1;
		$this->view->level2=$level2;
		$this->view->level3=$level3;
		$this->view->level4=$level4;
		

    }
    
    public function insertCharacterAction(){
    	$numiid=$this->getRequest()->getParam('numiid');
    	$levle0=$this->getRequest()->getParam($numiid.'_level0');
    	$levle1=$this->getRequest()->getParam($numiid.'_level1');
    	$levle2=$this->getRequest()->getParam($numiid.'_level2');
    	$levle3=$this->getRequest()->getParam($numiid.'_level3');
    	$levle4=$this->getRequest()->getParam($numiid.'_level4');
    	$data['numiid']=$numiid;
    	if(null!=$levle0){
    		$data['level0']=implode(',', $levle0);
    	}else{
    		$data['level0']="";
    	}
    	if(null!=$levle1){
	    	$data['level1']=implode(',', $levle1);
    	}else{
    		$data['level1']="";
    	}
    	if(null!=$levle2){
	    	$data['level2']=implode(',', $levle2);
    	}else{
    		$data['level2']="";
    	}
    	if(null!=$levle3){
	    	$data['level3']=implode(',', $levle3);
    	}else{
    		$data['level3']="";
    	}
    	if(null!=$levle4){
	    	$data['level4']=implode(',', $levle4);
    	}else{
    		$data['level4']="";
    	}
    	$dbItemsCharacterList=new Application_Model_DbTable_ItemsCharacterList();
    	$dbItemsCharacterList->insertNew($data);

    	echo('<br/>success');
    	$this->_redirect('admin/character-setup');
    }
    
    public function setPriorityAction(){
/*        $config=new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini",'production');
        $db = Zend_Db::factory($config->resources->db);
		$select=$db->select()->from('txy_taobao_items', array('num_iid'))->where('is_onsale<100');
        $res=$db->fetchAll($select);
        $dbTaobaoItems=new Application_Model_DbTable_TaobaoItems();
        foreach ($res as $re){
            $dbTaobaoItems->initRelItems($re['num_iid']);
        }*/
    	$utilCharacter=new Txy_Util_Character();
    	$dbItemPriotity=new Application_Model_DbTable_ItemPriority();
    	$dbItemsCharacerList=new Application_Model_DbTable_ItemsCharacterList();
    	$res=$dbItemsCharacerList->getAllItems();
    	foreach($res as $re){
	    	$relArray=$utilCharacter->setCharacterPriority($re['numiid']);
	    	foreach($relArray as $rel){
	    		$dbItemPriotity->updateRelItems($re['numiid'],$rel);
	    	}
    	}
    	
    }
    public function updatePriorityAction(){
        $config=new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini",'production');
        $db = Zend_Db::factory($config->resources->db);
		$select=$db->select()->from(array('a'=>'txy_taobao_items'))->joinLeft(array('b'=>'txy_item_characters_list'),'a.num_iid=b.numiid')->where(' a.is_onsale<100')->order('a.id desc');
		$adapter=new Txy_Extends_RelTaobaoPageinator($select);
		$paginator = new Zend_Paginator($adapter);
		$paginator->setCurrentPageNumber($this->_getParam('page'));
		$this->view->paginator = $paginator;
        
        
/*    	$dbItemPriotity=new Application_Model_DbTable_ItemPriority();
    	$sqlStr=$dbItemPriotity->getRelItems('14864626469');
    	$dbTabaoItems=new Application_Model_DbTable_TaobaoItems();
    	$mainItem=$dbTabaoItems->getItemsByNumiid('14864626469');
    	$this->view->mainItem=$mainItem;
    	$this->view->relItems=$dbTabaoItems->getRelItems($sqlStr);*/
        
    }


}

